{% import "utils.html" as utils %}
<form method="POST" action="/task/{{ '%s/settime' % task.id if task else 'new' }}">
  <h2 class="text-center">设置定时</h2>
  <hr>

  <div id="run-result"></div>
  <div class="checkbox">
    <label>
      <input type="checkbox" id="ontime_flg" name="ontime_flg" {% if ontime.sw %}  checked="" {% endif %}> 定时开关
    </label>
  </div>
  <select id="ontime_method">
    <option {% if ontime.mode == 'ontime' %} selected="selected" {% endif %} value="ontime">定时</option>
    <option {% if ontime.mode == 'cron' %} selected="selected" {% endif %} value="cron">cron</option>
  </select>
  <div id='ontime_content' {% if ontime.mode == 'ontime' %} style="display: block;" {% else %} style="display: none;" {% endif %}>
    <input id="ontime_run_date" type="date" min="{{today_date}}" value="{{today_date}}">
    <input type="time" id="ontime_val" value="{{ ontime.time }}" name='ontimevalue', step="1" />
  </div>
  <div id='cron_content' {% if ontime.mode == 'cron' %} style="display: block;" {% else %} style="display: none;" {% endif %}>
    <input type="text" class="form-control" id="cron_val" name="cron_val" {% if ontime.mode == 'cron'%} value="{{ontime.cron_val}}" {% endif %} id="cron_val" placeholder="仅支持5位标准 cron格式" >
    <label class="control-label">精确到秒：</label>
    <input type="number" id="cron_sec" name="cron_sec" value="{{ ontime.cron_sec }}" class="modal_input" min="0" max="59"/>
  </div>
  <div id="randtime_content">
    <div name="randzone readme">
      <label class="control-label">当天随机延时区间(0-604800)：</label>
    </div>
    <div name="randzone">
      <label>
        <input type="checkbox" id="randtimezonesw" name="randtimezonesw" {% if ontime.randsw %} checked="" {% endif %} >开关
      </label>
      <input type="number" id="randtimezone1" name="randtimezone1" value="{{ ontime.tz1 }}" class="modal_input" min="0" max="604800"/>~
      <input type="number" id="randtimezone2" name="randtimezone2" value="{{ ontime.tz2 }}" class="modal_input" min="0" max="604800"/>秒
    </div>
  </div>

  <div class="text-right">
    <button id="send" name="send" type="submit" data-loading-text="loading..." class="btn btn-primary">提交</button>
  </div>

  <script>
    $('#ontime_method').change(function() {
      if (this.value == 'ontime') {
        document.getElementById('ontime_content').style.display='block';
        document.getElementById('cron_content').style.display='none';
      } else if (this.value == 'cron') {
        document.getElementById('ontime_content').style.display='none';
        document.getElementById('cron_content').style.display='block';
      } else{
        document.getElementById('ontime_content').style.display='none';
        document.getElementById('cron_content').style.display='none';
        document.getElementById('randtime_content').style.display='none';
      }
  
      return false;
    });
  </script>
  <script>
    $('#send').on('click', function() {
      sendData = {}
      data = {}

      if ($('#randtimezonesw')[0].checked == true){
        sendData['tz1'] = parseInt($('#randtimezone1')[0].value);
        sendData['tz2'] = parseInt($('#randtimezone2')[0].value);
        if (sendData['tz1'] > sendData['tz2']){
            $('#run-result').html("<h1 class=\"alert alert-danger text-center\">随机值错误</h1><div><pre>开启随机时tz1 不能小于 tz2</pre></div>").show();
            return false;
        }
        sendData['randsw'] = true;
      }else{
        sendData['randsw'] = false;
      }

      if ($('#ontime_flg')[0].checked == true){
        sendData['mode'] = $('#ontime_method')[0].value;
        sendData['sw'] = true;
        if (sendData['mode'] == 'ontime'){
          sendData['date'] = $('#ontime_run_date')[0].value;
          sendData['time'] = $('#ontime_val')[0].value;
        } else if (sendData['mode'] == 'cron'){
          sendData['cron_val'] = $('#cron_val')[0].value;
          sendData['cron_sec'] = $('#cron_sec')[0].value;
        } else{
          $('#run-result').html("<h1 class=\"alert alert-danger text-center\">模式错误</h1><div><pre>模式错误</pre></div>").show();
          return false;
        }
      } else {
        sendData['sw'] = false;
      }

      var $this = $(this);
      $.ajax("/task/{{task.id}}/settime", {
            type: 'POST',
            data: sendData,
          })
          .done(function(data) {
            $('#run-result').html(data).show()
          })
          .always(function() {
          $this.button('reset');
        });
      return false;
    });
  </script>

  {{ utils.task_new_scripts() }}
  {{ utils.submit_loading() }}
</form>
